home *** CD-ROM | disk | FTP | other *** search
- /*
- IC Misc Subs.c
-
- */
-
- #include <stdarg.h>
- #include <Icons.h>
-
- #include "IC Globals.h"
- #include "IC Misc Subs.h"
-
- Str31 typed_chars;
- long typed_time;
- ListHandle typed_lh;
-
- OSErr PrepMem(void){
- long totalSize,contigSize;
-
- PurgeSpace(&totalSize,&contigSize);
-
- // totalSize is the total free space available if purge & compact took place
- // contigSize is largest contiguous block available if purge took place
-
- PurgeMem(contigSize); // purge memory to get the contigSize area
-
- CompactMem(totalSize);
-
- return noErr;
- }
-
- StringPtr SetPString(StringPtr str,short ct,...){
- str[0]=0;
-
- if (ct)
- vConcat(str,ct,__va(ct));
-
- return str;
- }
-
- StringPtr Delete(StringPtr str,short start,short end){
-
- // keep values within bounds
- if (start<1)
- start=1;
-
- if (end>255)
- end=255;
-
- if (str==(StringPtr)0)
- return str; // return null string
-
- // if string shorter than start, nothing to delete
- if (str[0]<start)
- return str;
-
- // if string shorter than tail of delete, we're truncating string
- if (str[0]<=end){
- str[0]=start-1;
- return str;
- }
-
- if (start==1){
- // move end down to front, string extends past end
- str[0]-=end;
- BlockMoveData((Ptr)&(str[end+1]),(Ptr)&(str[1]),str[0]);
- return str;
- }
-
- // start & end are in middle of string
- str[0] -= (end-start+1); // subtract chars being removed
- BlockMoveData((Ptr)&(str[end+1]),(Ptr)&(str[start]),(end-start+1));
- return str;
- }
-
- StringPtr NewPString(StringPtr str){
- StringPtr s;
-
- s=(StringPtr)NewPtr(256);
- s[0]=0;
-
- if (str!=(StringPtr)0)
- SetPString(s,1,str);
-
- return s;
- }
-
- StringPtr Concat(StringPtr str,short ct,...){
- return vConcat(str,ct,__va(ct));
- }
-
- StringPtr vConcat(StringPtr str,short ct,va_list v){
- StringPtr sp;
- unsigned char mv;
-
- if (str==(StringPtr)0)
- return str;
-
- while (ct){
- sp=va_arg(v,StringPtr);
-
- if (sp!=(StringPtr)0){
- mv=sp[0];
-
- if (mv+str[0]>255)
- mv=255-str[0];
-
- if (mv){
- BlockMoveData((Ptr)&(sp[1]),(Ptr)&(str[str[0]+1]),mv);
- str[0]+=mv;
- }
- }
- ct--;
- }
-
- return str;
- }
-
- void InitMiscSubs(void){
- typed_chars[0]=0;
- typed_time=0;
- typed_lh=(ListHandle)0;
- }
-
- StringPtr TPCopy(StringPtr to,StringPtr source,short start,short count){
- if (start<1){
- count=count-(1-start);
- start=1;
- }
-
- if (start+count>source[0])
- count=source[0]-start+1;
-
- if (count<0)
- count=0;
- to[0]=count;
- if (count)
- BlockMoveData((Ptr)&(source[start]),&(to[1]),count);
-
- return to;
- }
-
- short TPPos(StringPtr sub,StringPtr str){
- Handle sh=NewHandle(256);
- long pos;
-
- if (sh==(Handle)0)
- return -1;
-
- HLock(sh);
-
- BlockMoveData(str,*sh,str[0]+1);
-
- pos=Munger(sh,1,&(sub[1]),sub[0],(Ptr)0,0);
-
- HUnlock(sh);
- DisposeHandle(sh);
-
- return pos;
- }
-
- void StringToOSType(StringPtr s,OSType* otype){
- Str255 ts;
-
- SetPString(ts,2,s,"\p\0\0\0\0");
- BlockMoveData(&(ts[1]),otype,4);
- }
-
- void OSTypeToString(OSType otype,StringPtr s){
- BlockMoveData(&otype,&(s[1]),4);
- s[0]=4;
- }
-
- void DrawIcon(short id,Rect* r,Boolean highlighted){
- Handle suite,iconh;
- short transform;
-
- if ((System7)&&(GetIconSuite(&suite,id,svAllLargeData)==noErr)){
- if (highlighted)
- transform=ttSelected;
- else
- transform=ttNone;
-
- PlotIconSuite(r,0,transform,suite);
- DisposeIconSuite(suite,false);
- } else {
- iconh=Get1Resource('ICN#',id);
- if (iconh!=(Handle)0){
- PlotIcon(r,iconh);
- ReleaseResource(iconh);
- }
- }
- }
-
- OSErr FSWriteQ(short refnum,long count,Ptr buf){
- return FSWrite(refnum,&count,buf);
- }
-
- OSErr FSReadQ(short refnum,long count,Ptr buf){
- return FSRead(refnum,&count,buf);
- }
-
- short SelectedLine(ListHandle lh){
- Cell acell;
-
- SetPt(&acell,0,0);
-
- if (LGetSelect(true,&acell,lh))
- return acell.v;
-
- return -1;
- }
-
- Boolean IsKeyDown(short keycode){
- unsigned char mykeys[16];
- KeyMap* kmp;
-
- kmp=(KeyMap*)&mykeys;
-
- GetKeys(*kmp);
-
- return ((mykeys[keycode>>3]>>(keycode&7))&1);
- }
-
- OSErr ICGetVolInfo(StringPtr name,short* vrn,short index,long* CrDate){
- ParamBlockRec pb;
- OSErr oe;
-
- if ((name[0]!=0)&&(name[name[0]]!=':'))
- Concat(name,1,"\p:");
-
- pb.volumeParam.ioNamePtr=name;
- pb.volumeParam.ioVRefNum=*vrn;
- pb.volumeParam.ioVolIndex=index;
-
- oe=PBGetVInfoSync(&pb);
-
- if (oe==noErr){
- *CrDate=pb.volumeParam.ioVCrDate;
- *vrn=pb.volumeParam.ioVRefNum;
- }
-
- return oe;
- }
-
- OSErr MyGetAPPL(OSType sig,FSSpec* fs){
- short i=1;
- DTPBRec pbdt;
- long crdate;
- OSErr oe;
- Boolean found=false;
-
- if (System7){
- do {
- fs->vRefNum=0;
- fs->name[0]=0;
- oe=ICGetVolInfo(fs->name,&(fs->vRefNum),i++,&crdate);
- if (oe==noErr){
- fs->name[0]=0;
- pbdt.ioNamePtr=fs->name;
- pbdt.ioVRefNum=fs->vRefNum;
-
- oe=PBDTGetPath(&pbdt);
-
- if (oe==noErr){
- pbdt.ioIndex=0;
- pbdt.ioFileCreator=sig;
-
- oe=PBDTGetAPPLSync(&pbdt);
-
- found=(oe==noErr);
- }
- oe=noErr;
- }
- } while ((!found)&&(oe==noErr));
- }
-
- if (found){
- oe=noErr;
- fs->parID=pbdt.ioAPPLParID;
- } else {
- oe=afpItemNotFound;
- fs->vRefNum=0;
- fs->parID=2;
- fs->name[0]=0;
- }
-
- return oe;
- }
-
- OSErr ICFSpGetCatInfo(FSSpec* fs,short index,CInfoPBRec* pb){
- pb->hFileInfo.ioVRefNum=fs->vRefNum;
- pb->hFileInfo.ioDirID=fs->parID;
- pb->hFileInfo.ioNamePtr=fs->name;
- pb->hFileInfo.ioFDirIndex=index;
-
- return PBGetCatInfoSync(pb);
- }
-
- OSErr ICFSpSetCatInfo(FSSpec* fs,CInfoPBRec* pb){
- pb->hFileInfo.ioVRefNum=fs->vRefNum;
- pb->hFileInfo.ioDirID=fs->parID;
- pb->hFileInfo.ioNamePtr=fs->name;
-
- return PBSetCatInfoSync(pb);
- }
-
- RgnHandle GetWindowContentRegion(WindowPtr wind){
- return ((WindowPeek)wind)->contRgn;
- }
-
- RgnHandle GetWindowStructureRegion(WindowPtr wind){
- return ((WindowPeek)wind)->strucRgn;
- }
-
- Boolean TitleBarOnScreen(WindowPtr wp){
- RgnHandle rgn;
- Boolean ret;
-
- rgn=NewRgn();
-
- CopyRgn(GetWindowStructureRegion(wp),rgn);
- DiffRgn(rgn,GetWindowContentRegion(wp),rgn);
- SectRgn(rgn,GetGrayRgn(),rgn);
- ret=!EmptyRgn(rgn);
- DisposeRgn(rgn);
-
- return ret;
- }
-
- void GetWindowPortRect(WindowPtr wind,Rect* portRect){
- *portRect=((WindowPeek)wind)->port.portRect;
- }
-
- void GetWindowRect(WindowPtr wind,Rect* r){
- SetPort(wind);
- GetWindowPortRect(wind,r);
- LocalToGlobal(&(topLeft(*r)));
- LocalToGlobal(&(botRight(*r)));
- }
-
- void CentreRect(Rect* inside_rect,Rect* res_rect){
- Point stat_siz;
-
- stat_siz=botRight(*inside_rect);
- SubPt(topLeft(*inside_rect),&stat_siz);
- OffsetRect(res_rect,-res_rect->left,-res_rect->top);
- SubPt(botRight(*res_rect),&stat_siz);
- stat_siz.h/=2;
- stat_siz.v/=2;
- AddPt(topLeft(*inside_rect),&stat_siz);
- OffsetRect(res_rect,stat_siz.h,stat_siz.v);
- }
-
- void CentreAlert(short id){
- AlertTHndl alerth;
- Rect bounds;
-
- alerth=(AlertTHndl)GetResource('ALRT',id);
- if (alerth!=(AlertTHndl)0){
- bounds=qd.screenBits.bounds;
- bounds.bottom=(bounds.bottom-bounds.top)*2/3+bounds.top;
- HLock((Handle)alerth);
- CentreRect(&bounds,&((*alerth)->boundsRect));
- HUnlock((Handle)alerth);
- }
- }
-
- StringPtr GetAString(StringPtr dest,short id,short index){
- GetIndString(dest,id,index);
- return dest;
- }
-
- void Assert(Boolean b){
- if (!b)
- DebugStr("\pAssertion failure ; sc");
- }
-
- void SetItemEnable(MenuHandle mh,short item,Boolean enable){
- if (enable)
- EnableItem(mh,item);
- else
- DisableItem(mh,item);
- }
-
- Boolean DirtyKey(char ch){
- switch (ch){
- case homeChar: case endChar: case helpChar: case pageUpChar: case pageDownChar:
- case leftArrowChar: case rightArrowChar: case upArrowChar: case downArrowChar:
- return false;
- default:
- return true;
- }
- }
-
- OSErr IsVolumeWriteable(short vRefNum){
- HParamBlockRec pb;
- OSErr err;
-
- pb.volumeParam.ioVRefNum=vRefNum;
- pb.volumeParam.ioNamePtr=(StringPtr)0;
- pb.volumeParam.ioVolIndex=0;
-
- err=PBHGetVInfoSync(&pb);
-
- if (err==noErr){
- if (pb.volumeParam.ioVAtrb&0x0080)
- err=wPrErr; // volume locked by hardware
- else if (pb.volumeParam.ioVAtrb&0x8000)
- err=fLckdErr; // locked by software
- }
-
- return err;
- }
-
- OSErr IsFileWriteable(FSSpec* fs){
- CInfoPBRec pb;
- OSErr err;
-
- pb.hFileInfo.ioNamePtr=fs->name;
- pb.hFileInfo.ioVRefNum=fs->vRefNum;
- pb.hFileInfo.ioDirID=fs->parID;
- pb.hFileInfo.ioFDirIndex=0; // use ioNamePtr and ioDirID
- err=PBGetCatInfoSync(&pb);
-
- if (err==noErr)
- if (pb.hFileInfo.ioFlAttrib&0x01)
- err=fLckdErr;
-
- return err;
- }
-
- OSErr HGetDirAccess(short vRefNum,long dirID,StringPtr name,long* ownerID,long* groupID,long* accessRights){
- HParamBlockRec pb;
- OSErr err;
-
- pb.accessParam.ioNamePtr=name;
- pb.accessParam.ioVRefNum=vRefNum;
- pb.fileParam.ioDirID=dirID; // need to set offset 48
-
- err=PBHGetDirAccessSync(&pb);
-
- *ownerID=pb.accessParam.ioACOwnerID;
- *groupID=pb.accessParam.ioACGroupID;
- *accessRights=pb.accessParam.ioACAccess;
-
- return err;
- }
-
- Boolean FileLocked(FSSpec* fss){
- Boolean locked;
- long access,junk;
-
- locked=IsVolumeWriteable(fss->vRefNum)!=noErr;
-
- if (!locked)
- locked=IsFileWriteable(fss)!=noErr;
-
- if (!locked){
- if (HGetDirAccess(fss->vRefNum,fss->parID,(StringPtr)0,&junk,&junk,&access)==noErr)
- locked=!(access&(1L<<26));
- }
-
- return locked;
- }
-
- void LSetNoSelection(ListHandle list){
- Cell c;
-
- c.v=0;
- c.h=0;
-
- while (LGetSelect(true,&c,list)){
- LSetSelect(false,c,list);
- c.v++;
- c.h=0;
- }
- }
-
- void LSetSingleSelection(ListHandle list,short v){
- Cell c;
-
- c.h=0;
- c.v=v;
-
- LSetNoSelection(list);
- LSetSelect(true,c,list);
- LAutoScroll(list);
- }
-
- StringPtr LGetUniqueEntryName(ListHandle list,Cell* c,ListKeyUPP getentryname,StringPtr dest){
- Str31 ts;
-
- dest[0]=0;
-
- if (getentryname!=(ListKeyUPP)0)
- CallListKeyProc(getentryname,list,*c,dest);
-
- ts[0]=3;
- ts[1]=0;
- ts[2]=c->v/256;
- ts[3]=c->v%256;
-
- return Concat(dest,1,ts);
- }
-
- Boolean LGetFirstSelection(ListHandle list,Cell* c,ListKeyUPP getentryname){
- Str255 best,n;
- short index;
- Boolean ret=false;
-
- c->h=c->v=0;
- best[0]=2;
- best[1]=best[2]=255;
-
- while (LGetSelect(true,c,list)){
- ret=true;
-
- if (getentryname!=(ListKeyUPP)0)
- CallListKeyProc(getentryname,list,*c,n);
- else {
- n[0]=2;
- n[1]=n[2]=255;
- }
-
- if (IUCompString(n,best)<0){
- index=c->v;
- SetPString(best,1,n);
- }
- c->v++;
- }
- c->h=0;
- c->v=index;
-
- return ret;
- }
-
- Boolean LSelectFirstBefore(ListHandle list,const StringPtr s,ListKeyUPP getentryname){
- short i,index=0;
- Cell c;
- Str255 best="\p",n;
- Boolean good=false;
-
- for (i=0;i<(*list)->dataBounds.bottom-1;i++){
- c.h=0;
- c.v=i;
- if (getentryname!=(ListKeyUPP)0)
- CallListKeyProc(getentryname,list,c,n);
- else {
- n[0]=2;
- n[1]=n[2]=255;
- }
-
- if ((IUCompString(s,n)>0)&&(IUCompString(n,best)>0)){
- SetPString(best,1,n);
- index=c.v;
- good=true;
- }
- }
-
- if (good)
- LSetSingleSelection(list,index);
-
- return good;
- }
-
- Boolean LGetLastSelection(ListHandle list,Cell* c,ListKeyUPP getentryname){
- Str255 n,best="\p";
- short index;
- Boolean ret=false;
-
- c->v=c->h=0;
-
- while (LGetSelect(true,c,list)){
- ret=true;
- if (getentryname!=(ListKeyUPP)0)
- CallListKeyProc(getentryname,list,*c,n);
- else {
- n[0]=2;
- n[1]=n[2]=255;
- }
-
- if (IUCompString(n,best)>0){
- index=c->v;
- SetPString(best,1,n);
- }
- c->v++;
- }
- c->h=0;
- c->v=index;
-
- return ret;
- }
-
- Boolean LSelectFirstAfter(ListHandle list,const StringPtr s,ListKeyUPP getentryname,Boolean orequal){
- short i,index,compa;
- Cell c;
- Str255 best,n;
- Boolean good=false;
-
- best[0]=2;
- best[1]=best[2]=255;
-
- for (i=0;i<(*list)->dataBounds.bottom-1;i++){
- c.h=0;
- c.v=i;
- if (getentryname!=(ListKeyUPP)0)
- CallListKeyProc(getentryname,list,c,n);
- else {
- n[0]=2;
- n[1]=n[2]=255;
- }
- compa=IUCompString(s,n);
-
- if (((compa<0)||((compa==0)&&orequal))&&(IUCompString(n,best)<0)){
- SetPString(best,1,n);
- index=c.v;
- good=true;
- }
- }
- if (good)
- LSetSingleSelection(list,index);
-
- return good;
- }
-
- void DoListKey(ListHandle list,EventRecord* er,ListKeyUPP getentryname){
- Cell c;
- short index;
- Boolean dummy,found;
- long curticks=er->when;
- char ch;
- Str255 s;
-
- ch=er->message&0x00ff;
-
- if ((typed_lh!=list)||(ch<' ')){
- typed_time=0;
- typed_lh=list;
- }
-
- switch (ch){
- case downArrowChar:
- c.h=c.v=index=0;
- while (LGetSelect(true,&c,list)){
- c.v++;
- index=c.v;
- }
- if (index>(*list)->dataBounds.bottom)
- index=(*list)->dataBounds.bottom;
-
- LSetSingleSelection(list,index);
- LAutoScroll(list);
- break;
- case upArrowChar:
- c.h=c.v=0;
- if (!LGetSelect(true,&c,list))
- c.v=(*list)->dataBounds.bottom;
- if (c.v>0)
- c.v--;
- LSetSingleSelection(list,c.v);
- LAutoScroll(list);
- break;
- case homeChar:
- LScroll(0,-(*list)->dataBounds.bottom,list);
- break;
- case endChar:
- LScroll(0,(*list)->dataBounds.bottom,list);
- break;
- case pageUpChar:
- LScroll(0,-((*list)->visible.bottom-(*list)->visible.top-2),list);
- break;
- case pageDownChar:
- LScroll(0,((*list)->visible.bottom-(*list)->visible.top-2),list);
- break;
- case tabChar:
- if (er->modifiers&shiftKey){
- found=false;
- if (LGetFirstSelection(list,&c,getentryname)){
- if (getentryname!=(ListKeyUPP)0)
- CallListKeyProc(getentryname,list,c,s);
- if (LSelectFirstBefore(list,s,getentryname))
- found=true;
- }
- if (!found)
- if ((!LGetLastSelection(list,&c,getentryname))||(!LSelectFirstAfter(list,LGetUniqueEntryName(list,&c,getentryname,s),getentryname,false))){
- LSelectFirstAfter(list,"\p",getentryname,false);
- }
- }
- break;
- default:
- if (ch>' '){
- if (curticks-typed_time>60)
- typed_chars[0]=0;
- typed_time=curticks;
- typed_chars[0]++;
- typed_chars[typed_chars[0]]=ch;
- if (!LSelectFirstAfter(list,typed_chars,getentryname,true)){
- Str31 t;
- t[0]=1;
- t[1]=255;
- LSelectFirstBefore(list,t,getentryname);
- }
- }
- break;
- }
- }
-
- StringPtr DecStr(long l,StringPtr s){
- NumToString(l,s);
- return s;
- }
-
- StringPtr VersionStr(long lver,StringPtr s){
- NumVersion ver;
- Str255 tmp;
- long maj;
-
- s[0]=0;
- *((long*)&ver)=lver;
-
- if (ver.majorRev<=9)
- maj=ver.majorRev;
- else {
- maj=(ver.majorRev >> 4)&0x0f;
- maj*=10;
- maj+=(ver.majorRev&0x0f);
- }
- DecStr(maj,s);Concat(s,1,"\p.");
-
- maj=(ver.minorAndBugRev>>4)&0x0f;
- Concat(s,2,DecStr(maj,tmp),"\p.");
-
- maj=(ver.minorAndBugRev&0x0f);
- Concat(s,1,DecStr(maj,tmp));
-
- // now the stage
- if (ver.stage&0x20)
- Concat(s,1,"\pd");
- else if (ver.stage&0x40)
- Concat(s,1,"\pa");
- else if (ver.stage&0x60)
- Concat(s,1,"\pb");
- else if (ver.stage&0x80)
- Concat(s,1,"\pr");
- else
- Concat(s,1,"\px");
-
- maj=ver.nonRelRev;
- return Concat(s,1,DecStr(maj,tmp));
- }
-
- OSErr GetDirName(FSSpec* fs){
- CInfoPBRec pb;
-
- pb.hFileInfo.ioNamePtr=fs->name;
- pb.hFileInfo.ioVRefNum=fs->vRefNum;
- pb.hFileInfo.ioDirID=fs->parID;
- pb.hFileInfo.ioFDirIndex=-1;// get information about ioDirID;
- return PBGetCatInfoSync(&pb);
- }
-
- OSErr CopyFork(short srn,short drn,long len){
- OSErr err=noErr;
- Ptr p=(Ptr)0;
- long size=65536,count;
-
- do {
- p=NewPtr(size);
- if (p!=(Ptr)0)
- size/=2;
- } while ((p==(Ptr)0)&&(size>=512));
-
- if (p==(Ptr)0)
- err=memFullErr;
-
- while ((err==noErr)&&(len>0)){
- count=size;
- if (count>len)
- count=len;
-
- err=FSRead(srn,&count,p);
- if ((err==noErr)&&(count==0))
- err=eofErr;
- if (err==noErr){
- len -= count;
- err=FSWrite(drn,&count,p);
- }
- }
-
- if (p!=(Ptr)0)
- DisposePtr(p);
-
- return err;
- }
-
- OSErr CopyFile(FSSpec* source,FSSpec* dest){
- OSErr err,oerr;
- CInfoPBRec pb;
- short srrn,sdrn,drrn,ddrn;
-
- HDelete(dest->vRefNum,dest->parID,dest->name);
- if ((err=ICFSpGetCatInfo(source,0,&pb))==noErr){
- if ((err=HCreate(dest->vRefNum,dest->parID,dest->name,pb.hFileInfo.ioFlFndrInfo.fdCreator,pb.hFileInfo.ioFlFndrInfo.fdType))==noErr){
- if ((err=HOpen(dest->vRefNum,dest->parID,dest->name,fsWrPerm,&ddrn))==noErr){
- if ((err=HOpenRF(dest->vRefNum,dest->parID,dest->name,fsWrPerm,&drrn))==noErr){
- if ((err=HOpen(source->vRefNum,source->parID,source->name,fsRdPerm,&sdrn))==noErr){
- if ((err=HOpenRF(source->vRefNum,source->parID,source->name,fsRdPerm,&srrn))==noErr){
- if ((err=CopyFork(sdrn,ddrn,pb.hFileInfo.ioFlLgLen))==noErr){
- if ((err=CopyFork(srrn,drrn,pb.hFileInfo.ioFlRLgLen))==noErr){
- // if no error copy complete
- }
- }
- FSClose(srrn);
- }
- FSClose(sdrn);
- }
- FSClose(drrn);
- }
- FSClose(ddrn);
- }
- }
- }
-
- if (err==noErr)
- err=ICFSpSetCatInfo(dest,&pb);
- if (err!=noErr)
- HDelete(dest->vRefNum,dest->parID,dest->name);
-
- return err;
- }
-
- StringPtr GetName(short id1,short id2,StringPtr name){
- StringHandle sh;
-
- sh=GetString(id1);
- if (sh==(StringHandle)0)
- sh=GetString(id2);
-
- if (sh==(StringHandle)0)
- SetPString(name,1,"\punnamed");
- else
- SetPString(name,1,*sh); // don't release the string handle, someone else may be using it
-
- return name;
- }
-
- StringPtr GetOwnerName(StringPtr name){
- return GetName(owner_id,machine_id,name);
- }
-
- StringPtr GetMachineName(StringPtr name){
- return GetName(machine_id,owner_id,name);
- }
-
-